package com.cheer.ba.view.activity;

import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.cheer.ba.R;
import com.cheer.ba.constant.H5Address;
import com.cheer.ba.constant.IntentKey;
import com.cheer.ba.event.BaseEvent;
import com.cheer.ba.utils.SystemUtils;
import com.cheer.ba.view.base.BaseCommAty;
import com.tencent.bugly.beta.Beta;
import com.tencent.bugly.beta.UpgradeInfo;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 关于加油啦
 */
public class AboutCheerAty extends BaseCommAty {

    //当前版本
    @BindView(R.id.id_version_code_txt)
    TextView mTxtVersion;

    //是否有更新
    @BindView(R.id.id_app_update_txt)
    TextView mTxtUpdate;


    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.aty_about_cheer, null, false));
    }

    @Override
    public void initView() {
        setTitleText("关于加油啦");
        mTxtVersion.setText("当前版本:" + SystemUtils.getVersionName(aty) + "v");
        mTxtUpdate.setText("已最新");
        EventBus.getDefault().register(this);
    }

    @Override
    public void initData() {
        /***** 获取升级信息 *****/
        UpgradeInfo upgradeInfo = Beta.getUpgradeInfo();
        if (upgradeInfo != null) {
            BaseEvent event = new BaseEvent();
            event.type = IntentKey.Upgrade.KEY_HAD_UPDATE;
            EventBus.getDefault().post(event);
        } else {
            BaseEvent event = new BaseEvent();
            event.type = IntentKey.Upgrade.KEY_NO_UPDATE;
            EventBus.getDefault().post(event);
        }
    }

    @OnClick({R.id.id_layout_update, R.id.id_about_us_view})
    public void onClick(View mView) {
        switch (mView.getId()) {
            //版本更新
            case R.id.id_layout_update:
                /***** 检查更新 *****/
                Beta.checkUpgrade();

                /***** 获取升级信息 *****/
                UpgradeInfo upgradeInfo = Beta.getUpgradeInfo();
                if (upgradeInfo != null) {
                    BaseEvent event = new BaseEvent();
                    event.type = IntentKey.Upgrade.KEY_HAD_UPDATE;
                    EventBus.getDefault().post(event);
                } else {
                    BaseEvent event = new BaseEvent();
                    event.type = IntentKey.Upgrade.KEY_NO_UPDATE;
                    EventBus.getDefault().post(event);
                }
                break;
            //关于我们
            case R.id.id_about_us_view:
                CommH5WithTitleAty.startCommonH5(aty, H5Address.ABOUT_US, "关于我们", false, false, "", "", "");
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventUpdate(BaseEvent event) {
        if (TextUtils.equals(IntentKey.Upgrade.KEY_HAD_UPDATE, event.type)) {
            mTxtUpdate.setText("有更新");
            mTxtUpdate.setTextColor(Color.RED);
        } else if (TextUtils.equals(IntentKey.Upgrade.KEY_NO_UPDATE, event.type)) {
            mTxtUpdate.setText("已最新");
            mTxtUpdate.setTextColor(getResources().getColor(R.color.color_999999));
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
